<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Like;
use api\wxapp\model\Collection;
use api\wxapp\model\Comment;
use api\wxapp\model\CommentReply;
class LikeController extends AdminBaseController
{
    private $shop_id;
    private $where = array();
    private $params = array();
    public function initialize()
    {
        parent::initialize();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $idarr = db::name('app_shop')->alias('t1')
                ->join('app_shopgoods t2','t2.shop_id = t1.id')
                ->where('t1.admin_id',$sessionAdminId)
                ->column('t2.goods_id');
            $this->where[] = ['t2.goods_id','in',$idarr];
        }

        $this->params = $this->param();
        if(isset($this->params['name'])){
            $this->where[] = ['t3.name','like','%'.$this->params['name'].'%'];
        }
    }
    //点赞
    public function index(Like $like)
    {
        if($this->where && !$this->where[0][2]){
            $list = array();
        }else{
            $query = Like::alias('t1');
            $query = $this->querydo($query,$like);
            $list = $query->paginate(10);
            $list->appends($this->params);
            $this->assign('page', $list->render());
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
    //收藏
    public function collection(Like $like)
    {
        if($this->where && !$this->where[0][2]){
            $list = array();
        }else{
            $query = Collection::alias('t1');
            $query = $this->querydo($query,$like);
            $list = $query->paginate(10);
            $list->appends($this->params);  //分页条件
            $this->assign('page', $list->render());
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
    //评论
    pubLic function comment(Like $like)
    {
        if($this->where && !$this->where[0][2]){
            $list = array();
        }else{
            $query = Comment::alias('t1');
            $query = $this->querydo($query,$like);

            $list = $query->paginate(10,false,['query'=>$this->params])->each(function($item, $key){
                    $find = CommentReply::where('comment_id',$item['id'])->value('content');
                    if($find){
                        $item['replycontent'] = $find;
                    }else{
                        $item['replycontent'] = '无';
                    }
                });
            $list->appends($this->params);  //分页条件
            $this->assign('page', $list->render());
        }
    
        $this->assign('list',$list);
        return $this->fetch();
    }
    //
    private function querydo($query,$like)
    {
        $query = $like->leftuser($query);
        $query = $like->leftgoods($query);
        $query = $query->where($this->where);
        $query =  $query->field('t1.*, t2.nickname,t3.name');
        return $query;
    }
}
